<?php
/**
*+-------IMBA游戏业务数据的获取操作--------+
*+-------------------------------------------------+
*+-------游戏业务是基于MongoDB存储的---------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/
namespace Api\Event;

class GameEvent extends BaseEvent {

	/**
	*模型对象
	*/
	protected $mod;

	/**
	*成员变量的初始化
	*/
	protected function _initialize(){
		parent::_initialize();
		$this->mod = D('Common/Game', 'Service');
	}
    
    /**
    *游戏列表数据
    *@param null
    *@return array
    */
    // 2015-4-13 18:55:34 Legend.   
    // 修改list => lists    list是预设函数 不能写作为方法
    public function lists($query = '', $field='', $limit=0, $p=0, $orderby='sort asc'){
    	$map = array('status'=>array('gt', 0),);
    	$query && $map = array_merge($map, $query);

    	$res = $this->mod->where($map)->field($field)->order($orderby);
    	if($limit)
    		$res = $res->limit($limit);
    	if($p)
    		$res = $res->page($p);
    	return $res->select();
    }

    /** 
	 * 取总游戏数
	 * @author susy<susy.liang@ilongyuan.com.cn>
	 */
	
	public function total()
	{

		$map = array('status'=>array('gt', 0));

		return $this->mod->where($map)->count();
	}


    /** 
     * 获取展示的游戏列表
     * @author Legend. <xcx_legender@qq.com>
     * @param 
     * @return 
     */
    
    public function showLists($query = [])
    {
    	$map = array('status'=>array('gt', 0),);
        $query && $map = array_merge($map, $query);
    	return $this->mod->where($map)->order('sort asc')->select();
    }

    /**
	*游戏具体明细信息
	*@param int $id 游戏标识
	*@return array 明细数据
	*/
	public function info($id = 0)
	{
		$game = !empty(self::$STATIC['game'][$id]) ? self::$STATIC['game'][$id] : null;
		if(!$game){
			$service = D('Common/Game', 'Service');
			$map = array();
			$map['status'] = array('gt', 0);
			$map['id']     = intval($id);
			$game = $service->where($map)->find();

			unset($game['_id']);
			if ($game)
			{
				$game['pic'] = $game['pic']?$game['pic']:'';
				$game['logo'] = fieldOrNull($game, 'logo');//?$game['logo']:'';
				$game['background'] = $game['background']?$game['background']:'';
			}
			self::$STATIC['game'][$id] = $game;
		}
		
		return $game;
	}


	/** 
	 * 设置订阅数增减1
	 * @author Legend. <xcx_legender@qq.com>
	 * @param int $game_id, boolean $plus 默认增加 为假则减
	 * @return 
	 */
	
	public function scribe($game_id, $plus = true)
	{
		$where['id'] = $game_id;
		$act = $plus ? 'setInc' : 'setDec';
		return $this->mod->where($where)->$act('scribe', 1);
	}

	/**
	*分页进行游戏数据的获取
	*@author susy.liang<susy.liang@ilongyuan.com.cn>
	*/
	public function getGames($listNum=16, $timestamp=0, $field=''){

		$service = D('Common/Game', 'Service');

		$order = 'create_time desc';

		$map = array();
		$map['$and'][]['status']	= $service::STATUS_PASS;
		
		$timestamp && $map['$and'][]['create_time'] =  array('$lt' => $timestamp);
		$where = array('_array'=>$map);

		$lists = $service->where($where)->order($order)->limit($listNum)->field($field)->select();

		return $lists;

	}

	public function getOptions()
	{
		$map['status'] = 1;
		$games = service('Game')->where($map)->select();
		foreach ($games as $game) {
			$data[$game['id']] = $game['name'];
		}
		return $data;
	}


    public function getGamesSortByScribe($ids = [])
    {
        $match = [];
        if (!empty($ids)) {
            $match = ['game_id' => ['$in' => $ids]];
        }
        $sort = ['count' => -1];
        $list = service('GameScribe')->total($match, 'game_id', 1, $sort);
        return $list;
    }
}